<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
       <template v-slot="{data}">
        <el-row style="width: 100%;height:40px" type="flex" justify="space-between" align="middle">
          <!-- 节点 -->
          <el-col >{{ data.name }}</el-col>
          <!-- 负责人 -->
          <el-col :span=" 4">
            <span class = 'tree-manager'>{{ data.managerName }}</span>
            <!-- 下拉框 -->
            <el-dropdown @command="operateDept($event,data.id)">
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子节点</el-dropdown-item>
                <el-dropdown-item command="edit">编辑节点</el-dropdown-item>
                <el-dropdown-item command="del">删除节点</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
       </template>
      </el-tree>
      <addDept ref="addDept" :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepartment="this.getDepartment"/>
        <!--添加编辑的弹框 -->
    </div>
  </div>
</template>
<script>
import {getDepartment ,delDepartment} from  '@/api/department'
import {transListToTreeData} from '@/utils'
import addDept from './components/add-dept.vue'
export default { 
  name: 'Department',
  components: {addDept},
  data(){
    return{
      //节点数据
      depts:[],
      //定义节点数据标签
      defaultProps:{
        label:'name',
        children:'children',
        managerName:'managerName'
      },
      //弹出的显示与隐藏
      showDialog:false,
      //传递的父id
      currentNodeId:null
    }
  },
  created(){
      this.getDepartment();
  },
  methods:{
    //获取部门信息
    async getDepartment(){
      const department = await getDepartment()
      this.depts =  transListToTreeData(department,0)
    },
    operateDept(command,pid){
      if(command ==='add'){
        this.showDialog = true,
        this.currentNodeId = pid
      }else if(command ==='edit'){
        this.showDialog = true
        this.currentNodeId = pid
        this.$nextTick(()=>{
          this.$refs.addDept.getDepartmentDetail()
        })
      }else{
       // 删除部门
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(pid)
          this.$message.success("删除成功")
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
  .app-container {
    padding: 30px 140px;
    font-size: 14px;
  }
  .tree-manager {
    width: 100px;
    display: inline-block;
    margin: 10px;
}
</style>
